import { getToken } from './auth.js'
import db from '@/common/localstorage.js'

let loginOut = false
const request = {
  put(url, data) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + url,
        header: {
          Authorization:db.get('appToken'),
		  appToken: db.get('appToken'),
        },
        data: data,
        method: 'PUT',
        success: function (res) {
          if (res.data.code == 401) {
            timeOut()
          }
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  post(url, data) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + url,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Authorization: db.get('appToken'),
          appToken: db.get('appToken'),
        },
        data: data,
        method: 'POST',
        success: function (res) {
          if (res.data.code == 401) {
            timeOut()
          }
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },

  postjson(url, data) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + url,
        header: {
         Authorization: db.get('appToken'),
         appToken: db.get('appToken'),
        },
        data: data,
        method: 'POST',
        success: function (res) {
          if (res.data.code == 401) {
            timeOut()
          }
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  postjsonNot(url, data) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + url,
        header: {
         Authorization: db.get('appToken'),
         appToken: db.get('appToken'),
        },
        data: data,
        method: 'POST',
        success: function (res) {
         resolve(res)    
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  get(url, data) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + url,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
         Authorization: db.get('appToken'),
         appToken: db.get('appToken'),
        },
        data: data,
        method: 'GET',
        success: function (res) {
          if (res.data.code == 401) {
            timeOut()
          }
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },

  getDict(type) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + '/admin-api/system/dict-data/page',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
         Authorization: db.get('appToken'),
         appToken: db.get('appToken'),
        },
        data: { pageNo: 1, pageSize: 100, dictType: type },
        method: 'GET',
        success: function (res) {
          if (res.data.code == 401) {
            timeOut()
          }
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  }
}

function timeOut() {
  uni.hideLoading()
  uni.showModal({
    title: '提示',
    content: '登录已过期，请重新登录',
    showCancel: false,
    success: function (res) {
      if (res.confirm) {
        uni.navigateTo({
          url: '/pages/login/index'
        })
        loginOut = true
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}
export default request
